package seven.test_7_8;

import org.omg.IOP.TAG_RMI_CUSTOM_MAX_STREAM_FORMAT;

import java.util.ArrayList;
import java.util.Scanner;

public class TestMain4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        String str1 = scanner.next();
        if (str1 == null) {
            System.out.println(-1);
            return;
        }
        String str2 = scanner.next();
        if (str2 == null) {
            System.out.println(-1);
            return;
        }
        String[] strings = new String[n];
        for (int i = 0; i < n; i++) {
            strings[i] = scanner.next();
        }
        int a = -1;
        int b = -1;
        int min = n;
        for (int i = 0; i < n; i++) {
            String str = strings[i];
            if (str.equals(str1)) {
                if (Math.abs(a - b) < min) {
                    min = Math.abs(a - b);
                    a = i;
                }
            }
            if (str.equals(str2)) {
                if (Math.abs(a - b) < min) {
                    min = Math.abs(a - b);
                    b = i;
                }
            }
        }
        if (a == -1 || b == -1) {
            System.out.println(-1);
            return;
        }
        System.out.println(min);
    }
}
